from MineAgent import MineAgent
from MineMove import MineMove
from MineInfo import MineInfo

class SimpleAgent(MineAgent):
    """A simple stupid agent to try the python implementation of the minesweeper
    client. This should be used as an example of how to implement a client.
    Look inside MineAgent.py to see how to implement each method."""

    def __init__(self):
        self.opened = None
        self.xSize = 0
        self.ySize = 0
        self.tryPeeking = True

    def startNewGame(self,numPlayers, playerID,
                     xSize,ySize,
                     deadCost,peekCost):
        self.xSize = xSize
        self.ySize = ySize
        self.opened = [False]*(xSize*ySize)
        return True

    def getNextMove(self):
        mineMove = MineMove(MineMove.OPEN_SQUARE)
        
        if self.tryPeeking is True:
            mineMove.move = MineMove.PEEK_SQUARE
            mineMove.X = 0
            mineMove.Y = 0
            print "Peeking"
            return mineMove

        for i in xrange(self.xSize):
            for j in xrange(self.ySize):
                if self.opened[i+j*self.xSize] is False:
                    mineMove.X = i
                    mineMove.Y = j
                    self.opened[i+j*self.xSize] = True
                    return mineMove
        
        mineMove.move = MineMove.STOP
        return mineMove

    def handleInfo(self,mineInfo):
        if mineInfo.infoType == MineInfo.ALREADY_PEEKED:
            self.tryPeeking = False
            print "Not allowed to peek anymore"
        elif mineInfo.infoType == MineInfo.SQUARE_INFO:
            print "Square info: " + str(mineInfo.value)
